﻿using System.Xml;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication.RSS
{
    public class RSSItem : IFeedItem 
    {  

        public string Title { get { return this._Title; } set { this._Title = value; } }
        public string Description { get { return this._Description; } set { this._Description = value; } }
        public Uri Comments { get { return this._Comments; } set { this._Comments = value; } }
        public Uri Link { get { return this._Link; } set { this._Link = value; } }
        public Guid Guid { get { return this._Guid; } set { this._Guid = value; } }
        public DateTime PublishDate { get { return this._PublishDate; } set { this._PublishDate = value; } }

        private Guid _Guid = Guid.Empty;
        private string _Title = string.Empty;
        private string _Description = string.Empty;
        private Uri _Comments = null;
        private Uri _Link = null;
        private DateTime _PublishDate = DateTime.MinValue;

        public RSSItem(string title, string description, Uri link, DateTime publishdate, Guid Guid)
        {
            this._Comments = null;
            this._Guid = Guid;
            this._Title = title.Trim();
            this._Description = description.Trim();
            this._Link = link;
            this._PublishDate = publishdate;
        }

        public RSSItem(string title, string description, string link, DateTime publishdate, Guid Guid)
        {
            this._Guid = Guid;
            this._Title = title.Trim();
            this._Description = description.Trim();
            this._Link = new Uri(link);
            this._PublishDate = publishdate;
            this._Comments = null;
        }


        public RSSItem(string title, string description, Uri link, Uri comments, DateTime publishdate, Guid Guid)
        {
            this._Comments = comments;
            this._Guid = Guid;
            this._Title = title.Trim();
            this._Description = description.Trim();
            this._Link = link;
            this._PublishDate = publishdate;
        }

        public RSSItem(string title, string description, string link, string comments, DateTime publishdate, Guid Guid)
        {
            this._Guid = Guid;
            this._Title = title.Trim();
            this._Description = description.Trim();
            this._Link = new Uri(link);
            this._PublishDate = publishdate;
            this._Comments = new Uri(comments);
        }

        public RSSItem()
        {
            this._Comments = null;
            this._Guid = Guid.NewGuid();
            this._Title = string.Empty;
            this._Description = string.Empty;
            this._Link = null;
            this._PublishDate = DateTime.Now;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<item>");
            sb.AppendLine(string.Format("<title>{0}</title>", FeedHelper.XmlEncode(this._Title)));
            sb.AppendLine(string.Format("<description>{0}</description>", FeedHelper.XmlEncode(this._Description)));
            if (this._Link != null) { sb.AppendLine(string.Format("<link>{0}</link>", this._Link.OriginalString)); } else { sb.AppendLine("<link></link>"); }
            if (this._Comments != null) { sb.AppendLine(string.Format("<comments>{0}</comments>", this._Comments.OriginalString)); } else { sb.AppendLine("<comments></comments>"); }
            sb.AppendLine(string.Format("<pubDate>{0}</pubDate>", this._PublishDate.ToString("r")));
            sb.AppendLine("</item>");
            return sb.ToString();
        }

        public XmlNode ToXml(XmlDocument Document)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, "item", "");
            node.AppendChild(FeedHelper.CreateElement(Document, "title", this._Title));
            node.AppendChild(FeedHelper.CreateElement(Document, "description", this._Description));
            if (this._Link != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", this._Link.OriginalString));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", string.Empty));
            }

            if (this._Comments != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "comments", this._Comments.OriginalString));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "comments", string.Empty));
            }

            node.AppendChild(FeedHelper.CreateElement(Document, "pubDate", this._PublishDate.ToString("r")));
            return node;
        }

    }
}
